$(function(){
	$("#zh_languageId,#en_languageId").on("change", function(){
		var language = $(this).children("option:selected").attr("data-key");
		if(language == 'zh' ){
			$("#en_languageId option[data-key='en']").attr("selected", true);
			$(".zh-product").addClass("defaultForm");
			$(".en-product").removeClass("defaultForm");
		}else{
			$("#zh_languageId option[data-key='zh']").attr("selected", true);
			$(".zh-product").removeClass("defaultForm");		
			$(".en-product").addClass("defaultForm");
		}
	})
	
	//$(".datepicker").datebind();
	$(".datepicker").datetimepicker({
		dateFormat: 'yy-mm-dd'
	});
	//set default date
	$('.datepicker').datetimepicker('setDate', (new Date()) );
	var uploadify = $("#zh_uploadFileSetInfoId,#en_uploadFileSetInfoId").uploadify({
		swf: ctx+"/extends/uploadify/uploadify.swf",
		uploader: ctx+ "/product/upload.do",
		fileTypeExts : "*.png;*.jpg;*.gif",
		fileSizeLimit:"10MB",
		removeCompleted: false,
		uploadLimit : 5,
		onSWFReady:function(){
			$(this).append("<input type='button' value='peizhi'>");
		},
		onUploadSuccess : function(file, data, response){
			var entity = $.parseJSON(data);
			var elementId = $(this)[0].original.attr("id");
			if(entity){
				if(elementId){
					elementId = elementId.replace("_",".");
				}
				if(!$("input[name='"+elementId+"']").val()){
					$("input[name='"+elementId+"']").val(entity.uploadFileSetInfoId);
				}
				$("#"+file.id).find(".cancel a").click(function(){
					deletePic(entity.uploadFileInfoId);
				})
			}
		},
		onUploadError : function(file, errorCode, errorMsg){

		},onUploadStart: function(file){
			var elementId = $(this)[0].original.attr("id");
			var uploadFileSetId = $("input[name='"+elementId.replace("_",".")+"']").val();
			$("#"+elementId).uploadify('settings','formData', {'uploadFileSetId' : uploadFileSetId});
		}
	});
	function deletePic(uploadFileInfoId){
		$.ajax({
			url: ctx+"/product/deleteUploadFile.do",
			type: "post",
			data:{uploadFileInfoId : uploadFileInfoId},
			success: function(d){
				
			},
			error: function(e){
				
			}
		})
	}
	
	$("#zh_uploadFileSetInfoId_history .uploadify-queue-item,#en_uploadFileSetInfoId_history .uploadify-queue-item").each(function(){
		var uploadFileInfoId = $(this).attr("data-uploadfileid");
		$(this).find(".cancel a").click(function(){
			deletePic(uploadFileInfoId);
		})		
	})	
	$(".setMainImage").on("click",function(){
		var showSetMainFile = $("#setMainFile").empty();
		var uploadFileSetInfoId = $(this).parents("div").find("input[name$='uploadFileSetInfoId']").val();
		if(!uploadFileSetInfoId)
			return;
		$.getJSON(ctx+"/product/getUploadFile.do",{fileSetInfoId:uploadFileSetInfoId},function(data){
			var ul = $("<ul>").appendTo(showSetMainFile);
			$.each(data, function(i, fileInfo){
				var checkbox = $("<input type='checkbox' value='"+fileInfo.uploadFileInfoId+"'>").attr("checked", fileInfo.isMainFile);
				$("<li>").append(checkbox).append(fileInfo.fileName).appendTo(ul);
				checkbox.on("click", function(){
					$(":checkbox").attr("checked", false);
					$(this).attr("checked", true);
				})
			})
		})
		showMessage({object: showSetMainFile, width:400,height:300},{OK:function(){
			var fileInfoId = $(":checkbox:checked").val();
			if(fileInfoId){
				$.ajax({
					url: ctx+"/product/setMainFile.do",
					data: {uploadFileInfoId: fileInfoId},
					type: "post",
					success:function(){}
				})
			}
		},
		Cancel:function(){}});
	})
})
